package com.qujia.station.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.exception.BaseException;
import com.qujia.common.exception.CustomException;
import com.qujia.station.domain.GzhFunctionConfig;
import com.qujia.station.domain.GzhFunctionTemplate;
import com.qujia.station.domain.dto.FunctionConfigTemplateDto;
import com.qujia.station.enums.GzhTypeEnum;
import com.qujia.station.mapper.GzhFunctionConfigMapper;
import com.qujia.station.mapper.GzhFunctionTemplateMapper;
import com.qujia.station.service.GzhFunctionConfigService;
import com.qujia.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service("gzhFunctionConfigService")
@Slf4j
public class GzhFunctionConfigServiceImpl implements GzhFunctionConfigService {

    @Resource
    private GzhFunctionConfigMapper gzhFunctionConfigMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private GzhFunctionTemplateMapper gzhFunctionTemplateMapper;

    @Override
    public List<FunctionConfigTemplateDto> selectGzhFunctionConfigList(Long userId) {

        GzhFunctionConfig gzhFunctionConfig = new GzhFunctionConfig();
        gzhFunctionConfig.setGzhType(GzhTypeEnum.REPORT.getValue());
        gzhFunctionConfig.setUserId(userId);
        SysUser user = sysUserMapper.selectUserById(userId);
        if(Objects.isNull(user)){
            throw new BaseException("用户不存在");
        }

        log.info("selectGzhFunctionConfigList 请求参数：[{}]", JSONObject.toJSONString(gzhFunctionConfig));
        List<FunctionConfigTemplateDto> functionConfigTemplateList = gzhFunctionConfigMapper.selectGzhFunctionConfigList(gzhFunctionConfig);
        if(CollectionUtils.isEmpty(functionConfigTemplateList)){
            return insertFunctionConfig(gzhFunctionConfig,user);
        }
        return functionConfigTemplateList;
    }

    @Override
    public List<FunctionConfigTemplateDto> selectGzhFunctionConfigList(GzhFunctionConfig gzhFunctionConfig) {
        return gzhFunctionConfigMapper.selectGzhFunctionConfigList(gzhFunctionConfig);
    }

    // 如果不存在就初始化
    private List<FunctionConfigTemplateDto> insertFunctionConfig(GzhFunctionConfig gzhFunctionConfig,SysUser sysUser){
        List<GzhFunctionTemplate> gzhFunctionTemplates = gzhFunctionTemplateMapper.selectAllGzhFunctionTemplateList(gzhFunctionConfig.getGzhType());
        for (GzhFunctionTemplate gzhFunctionTemplate : gzhFunctionTemplates) {
            GzhFunctionConfig functionConfig = new GzhFunctionConfig();
            functionConfig.setUserId(gzhFunctionConfig.getUserId());
            functionConfig.setGftId(gzhFunctionTemplate.getGftId());
            // 油站 员工 账号不支持一键条件
            if("一键调价".equals(gzhFunctionTemplate.getName()) && UserConstants.LEVEL_THREE.equals(sysUser.getUserLevel())){
                functionConfig.setStatus(0);
            }
            // 油企 账号不支持 扫码核销
            if("扫码核销".equals(gzhFunctionTemplate.getName()) && UserConstants.LEVEL_TWO.equals(sysUser.getUserLevel())){
                functionConfig.setStatus(0);
            }
            gzhFunctionConfigMapper.insertSelective(functionConfig);
        }
        return gzhFunctionConfigMapper.selectGzhFunctionConfigList(gzhFunctionConfig);
    }


    @Override
    public int updateGzhFunctionConfig(GzhFunctionConfig gzhFunctionConfig) {
        if(Objects.isNull(gzhFunctionConfig) || Objects.isNull(gzhFunctionConfig.getId())){
            throw new BaseException("请求参数异常");
        }
        GzhFunctionConfig config = new GzhFunctionConfig();
        config.setId(gzhFunctionConfig.getId());
        GzhFunctionConfig functionConfig = gzhFunctionConfigMapper.selectReportGzhFunctionConfig(config);
        if(Objects.isNull(functionConfig)){
            throw new BaseException("报表小程序功能配置不存在");
        }

        SysUser user = sysUserMapper.selectUserById(gzhFunctionConfig.getUserId());
        if(Objects.isNull(user)){
            throw new BaseException("用户不存在");
        }

        if(UserConstants.LEVEL_THREE.equals(user.getUserLevel()) && "一键调价".equals(functionConfig.getName())){
            throw new BaseException("该功能暂无权限启用，请联系区域经理");
        }
        if(UserConstants.LEVEL_TWO.equals(user.getUserLevel()) && "扫码核销".equals(functionConfig.getName())){
            throw new BaseException("该功能暂无权限启用，请联系区域经理");
        }

        // 记录第一次启用/停用状态变更的时间
        if(Objects.isNull(functionConfig.getOpenTime())){
            gzhFunctionConfig.setOpenTime(new Date());
        }
        return gzhFunctionConfigMapper.updateByPrimaryKeySelective(gzhFunctionConfig);
    }

    @Override
    public int updateMemberCenterConfig(GzhFunctionConfig gzhFunctionConfig) {
        return gzhFunctionConfigMapper.updateByPrimaryKeySelective(gzhFunctionConfig);
    }
}
